package com.bhkj.pdjhforotherapp.core.printutilspjq;

import com.bhkj.pdjhforotherapp.common.bean.JhResultBean;
import com.bhkj.pdjhforotherapp.common.bean.QueueBusiness;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.google.gson.reflect.TypeToken;
import com.gprinter.command.EscCommand;
import com.gprinter.command.LabelCommand;

import java.util.List;
import java.util.Vector;

/**
 * Created by Administrator on 2018/4/16.
 */

public class PrintContent {
    /**
     * 小票打印真实数据
     *
     * @return
     */
    public static Vector<Byte> getReceipt(JhResultBean jhResultBean, String queueBusiness) {
        EscCommand esc = new EscCommand();
        //初始化打印机
        esc.addInitializePrinter();
        //打印走纸多少个单位
        esc.addPrintAndFeedLines((byte) 1);
        // 设置打印居中
        esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);
        // 设置为倍高倍宽
        esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.ON, EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF);
        //设置字符右间距
        esc.addSetRightSideCharacterSpacing((byte)1);
        // 打印文字
        esc.addText(jhResultBean.getDatas().getDeptname() + "\n");
        //打印并换行
//        esc.addPrintAndLineFeed();
        // 取消倍高倍宽
        esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);
        // 设置打印左对齐
        esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);
        // 打印文字
        esc.addText("您的号码为:");
        addZoomMoreText(esc, "  "+jhResultBean.getDatas().getQunumber() + "\n");
        // 打印文字
        esc.addText("在您前面还有");
        addZoomMoreText(esc, " "+ jhResultBean.getDatas().getQueuenum()+" ");
        esc.addText("人在等待。\n");
        if (ObjectUtils.isNotEmpty(queueBusiness)){
            addYwAndBnum(esc, queueBusiness);
        }
//        esc.addText(jhResultBean.getDatas().getDeptname());
//        esc.addPrintAndLineFeed();
        esc.addText("请您在休息区等候\n");
        esc.addText("注意显示屏信息，过号无效，请您重新取号.\n");
        esc.addText("咨询热线:88958895 " + jhResultBean.getDatas().getPhone() + " \n");
        esc.addText(TimeUtils.getNowString());
        // 设置打印居右
        esc.addSelectJustification(EscCommand.JUSTIFICATION.RIGHT);
        //打印并换行
        esc.addPrintAndLineFeed();
        //打印走纸n个单位
        esc.addPrintAndFeedLines((byte) 2);
        // 开钱箱
        esc.addGeneratePlus(LabelCommand.FOOT.F2, (byte) 255, (byte) 255);
        //开启切刀
        esc.addCutPaper();
        //添加缓冲区打印完成查询
        byte[] bytes = {0x1D, 0x72, 0x01};
        //添加用户指令
        esc.addUserCommand(bytes);
        Vector<Byte> datas = esc.getCommand();
        return datas;
    }

    /**
     * 将文字的长和宽变为倍数
     */
    private static void addZoomMoreText(EscCommand esc, String text) {
        // 设置为倍高倍宽
        esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.ON, EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF);
        esc.addText(text);
        // 取消倍高倍宽
        esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);
    }

    /**
     * 添加选择的业务内容和笔数
     *
     * @param esc           打印机命令
     * @param queueBusiness 选择要办理的业务和笔数
     */
    private static void addYwAndBnum(EscCommand esc, String queueBusiness) {
        esc.addText("---------------------------\n");
        List<QueueBusiness> queueBusinesses = GsonUtils.fromJson(queueBusiness, new TypeToken<List<QueueBusiness>>() {
        }.getType());
        for (QueueBusiness param : queueBusinesses) {
            esc.addText(param.getName() + "   *   " + param.getBunum() + "笔\n");
        }
        esc.addText("---------------------------\n");
    }
}
